export const getCurrentDate = (): string => {
    const today = new Date()
    const formattedDate =
        today.getFullYear() +
        '-' +
        ('0' + (today.getMonth() + 1)).slice(-2) +
        '-' +
        ('0' + today.getDate()).slice(-2)
    return formattedDate
}

export const getLastWeekDate = (): string => {
    const today = new Date()
    const lastWeek = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000)
    const formattedDate =
        lastWeek.getFullYear() +
        '-' +
        ('0' + (lastWeek.getMonth() + 1)).slice(-2) +
        '-' +
        ('0' + lastWeek.getDate()).slice(-2)
    return formattedDate
}
// 书名显示前十个字+...+后六个字
export const shortenString = (str?: string): string => {
    return str
        ? str.length > 17
            ? `${str.slice(0, 10)}...${str.slice(-6)}`
            : str
        : ''
}

// 格式化时间
export const formatDateTime = (dateString: string): string => {
    if (!dateString) return ''
    return (dateString = dateString.replace('T', ' ').replace(/\..+Z/, ''))
}

// 根据淘宝和京东原图地址返回缩略图地址
export const getThumbnailUrl = (url: string): string => {
    let picUrl = ''
    if (typeof url !== 'string') {
        return picUrl
    }
    if (url.includes('alicdn.com')) {
        picUrl = url.replace('jpg_Q75.jpg', 'jpg_110x10000Q75.jpg')
    } else if (url.includes('360buyimg.com')) {
        picUrl = url.replace('/n12/', '/n5/')
    }
    return picUrl
}

export const numberFormat = (num: any) => {
    // 判断num是不是数字或者为空
    if (
        num === null ||
        typeof num === 'undefined' ||
        num === '' ||
        typeof num === 'object'
    ) {
        return ''
    } else {
        const value = Math.round(parseFloat(num) * 100) / 100
        const arrayNum = value.toString().split('.')

        if (arrayNum.length === 1) {
            return value.toString() + '.00'
        }

        if (arrayNum.length > 1) {
            if (arrayNum[1].length < 2) {
                return value.toString() + '0'
            }
        }
        return value.toString()
    }
}
export function isMp4Link(link: any) {
    if (link === null || link === '' || link === undefined) return false
    const videoExtensions = ['mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'mpeg']
    const extensionName = link.split('.').pop().toLowerCase()
    return videoExtensions.indexOf(extensionName.toLowerCase()) !== -1
}
